//
//  PlayListView.m
//  GC_OC
//
//  Created by wlf on 2022/5/16.
//

#import "PlayListView.h"
#import "PlayListTableViewCell.h"
#import "ICMusicPlayManager.h"
#import "PlayModel.h"
#import "PlayListModel.h"
@implementation PlayListView

- (void)showIn:(UIView *)view listArray:(nonnull NSArray *)listArray IdStr:(nonnull NSString *)idStr{
    [view addSubview:self];
    self.currentArray = [NSArray  array];
    self.currentArray = listArray;
    self.dataSource = [[NSUserDefaults standardUserDefaults] arrayForKey:@"getcatedetail"];
   
    self.currentTabView.currentArray = self.currentArray;
    self.historyTabView.currentArray = self.dataSource;
    for (NSDictionary *dict in self.currentArray) {
        NSNumber *dictID = dict[@"id"];
        if (dictID.integerValue == idStr.integerValue) {
            self.currentTabView.selectIndex = [self.currentArray indexOfObject:dict];
            break;
        }
    }
    for (NSDictionary *dict in self.dataSource) {
        NSNumber *dictID = dict[@"id"];
        if (dictID.integerValue == idStr.integerValue) {
            self.historyTabView.selectIndex = [self.dataSource indexOfObject:dict];
            break;
        }
    }

}
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeViewWithTap)];
        tap.delegate = self;
        [self addGestureRecognizer:tap];
        self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.2];
        UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 458, SCREEN_WIDTH, 458)];
        
        scrollView.delegate = self;
        scrollView.pagingEnabled=YES;
        scrollView.showsVerticalScrollIndicator=NO;
        scrollView.showsHorizontalScrollIndicator=NO;
        scrollView.backgroundColor = [UIColor clearColor];
        scrollView.contentSize=CGSizeMake((SCREEN_WIDTH - 17) * 2 + 6, 0);
        [self addSubview:scrollView];
        self.contentView = scrollView;
        
        [scrollView addSubview:self.historyTabView];
        self.historyTabView.frame = CGRectMake(SCREEN_WIDTH - 17 + 6, 0, SCREEN_WIDTH - 17, 458);
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.historyTabView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(12, 12)];
         CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = self.historyTabView.bounds;
         maskLayer.path = maskPath.CGPath;
        self.historyTabView.layer.mask = maskLayer;
      
        
        [scrollView addSubview:self.currentTabView];
        self.currentTabView.frame = CGRectMake(0, 0, SCREEN_WIDTH - 17, 458);
        
        UIBezierPath *maskPath1 = [UIBezierPath bezierPathWithRoundedRect:self.currentTabView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(12, 12)];
         CAShapeLayer *maskLayer1 = [[CAShapeLayer alloc] init];
         maskLayer1.frame = self.currentTabView.bounds;
         maskLayer1.path = maskPath1.CGPath;
        self.currentTabView.layer.mask = maskLayer1;
        
//       self.dataSource = [[NSUserDefaults standardUserDefaults] arrayForKey:@"getcatedetail"];
        self.historySelectindex = 0;
        self.currentSelectIndex = 0;
        
    }
    return self;
}
- (void)closeViewWithTap{
    
    [UIView animateWithDuration:0.3 animations:^{
           
           self.contentView.transform = CGAffineTransformMakeTranslation(0, 458);
       } completion:^(BOOL finished) {
           if (self.closeViewBlock) {
               self.closeViewBlock();
           }
           [ICMusicPlayManager sharedManager].narrowView.hidden = NO;
           [self.contentView removeFromSuperview];
           [self removeFromSuperview];
           
       }];
}

#pragma mark - UIGestureRecognizerDelegate

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    CGPoint point = [touch locationInView:self];
    if (point.y <= CGRectGetMinY(self.contentView.frame)) {
        
        return YES;
    }else{
        return NO;
    }
}
- (void)setSubTitleStr:(NSString *)subTitleStr{
    _subTitleStr = subTitleStr;
    _historyTabView.subTitleStr = subTitleStr;
}

- (PlayListTableView *)historyTabView{
    if (!_historyTabView) {
        _historyTabView = [[PlayListTableView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 458, SCREEN_WIDTH, 458) history:YES];
        WeakSelf(wself);
        _historyTabView.listPlayIndexBlock = ^(NSArray * _Nonnull dataSource, NSInteger index) {
            wself.historySelectindex = index;
            [wself PlayList:dataSource index:index];

        };
    }
    return _historyTabView;
}
- (PlayListTableView *)currentTabView{
    if (!_currentTabView) {
        _currentTabView = [[PlayListTableView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 458, SCREEN_WIDTH, 458) history:NO];
        WeakSelf(wself);
        _currentTabView.listPlayIndexBlock = ^(NSArray * _Nonnull dataSource, NSInteger index) {
            wself.currentSelectIndex = index;
            [wself PlayList:dataSource index:index];
        };
    }
    return _currentTabView;
}
- (void)PlayList:(NSArray *)dataSource index:(NSInteger)index{
//    [[PlayModel sharedModel] changeIndex:index];
//    ICMusicPlayManager *musicManager = [ICMusicPlayManager sharedManager];
//    //准备资源
//    [musicManager loadMusicSouceWithMusicArray:[NSMutableArray arrayWithArray:dataSource] Options:(ICMusicModePlayerOptions){.isNeedCyclePlay = NO,.isShowBackPlayInfo = YES,.playViewY = SCREEN_HEIGHT - 100,.playIndex = (int)[PlayModel sharedModel].currentIndex} Complete:^(ICMusicPlayModel * _Nullable model) {
//
//        [musicManager beginPlayUpDataViewWithModel:model NarrowViewStatue:ViewShow];
//    }];
    
//    [[GCAVPlayer shared] playIndex:index];
    [[GCAVPlayer shared] play:dataSource andindex:index ViewStatue:1 andIsAudio:YES];
    
    
    
}
@end
